package com.googlecode.websword.rest.spring;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

import com.googlecode.websword.rest.annotation.Rest;
import com.googlecode.websword.rest.annotation.RestSupport;

public class SpringRestSupport implements BeanPostProcessor {
    protected static transient Logger logger = Logger.getLogger(SpringRestSupport.class);

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Class<?> clz = bean.getClass();
        RestSupport restSupport = clz.getAnnotation(RestSupport.class);
        if(restSupport != null){
            Method[] mes = clz.getMethods();
            for(Method m : mes){
                Rest rest = m.getAnnotation(Rest.class);
                if(rest != null){
                    RestContainer.getContainer().registRouter(rest, m, beanName, clz);
                }
            }
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
